﻿using System.Collections.Generic;
using System.Globalization;
using System.Xml;
using Blackbird.Common.Properties;
using Blackbird.Common.Utility;
using Blackbird.Core.Editable.Base;

namespace Blackbird.Core.Editable.Frames
{
    public class EditableFrameset : EditableEntityCollection<EditableFrame>
    {
        private double _fps;
        private bool _loop;

        public bool Loop
        {
            get { return _loop; }
            set
            {
                _loop = value;
                InvokePropertyChanged("Loop");
            }
        }

        public double Fps
        {
            get { return _fps; }
            set
            {
                _fps = value;
                InvokePropertyChanged("Fps");
            }
        }

        public bool IsSorted { get; set; }

        public void AddRange(IEnumerable<EditableFrame> editableFrames)
        {
            IsSorted = false;
            foreach (var editableFrame in editableFrames)
                Items.Add(editableFrame);
        }

        public new void Add(EditableFrame item)
        {
            item.ParentFrameset = this;
            IsSorted = false;
            Items.Add(item);
        }

        public void Sort()
        {
            // TODO: This feels like cheating, but I don't want to spend the time 
            // writing a sort algorithm right now.
            var sortingFrames = new List<EditableFrame>(this);
            sortingFrames.Sort();
            Items.Clear();
            AddRange(sortingFrames);
            IsSorted = true;
        }

        protected override void ReadXmlContent(XmlReader reader)
        {
            reader.ReadStartElement("frames");
            var frames = new List<EditableFrame>();
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                var frame = new EditableFrame();
                reader.ReadStartElement("EditableFrame");
                frame.ReadXml(reader);
                reader.ReadEndElement();
                frames.Add(frame);
            }
            reader.ReadEndElement();
            frames.Sort();
            AddRange(frames);
        }      
        protected override void WriteXmlContent(XmlWriter writer)
        {
            writer.WriteStartElement("frames");
            foreach (EditableFrame frame in this)
            {
                writer.WriteStartElement("EditableFrame");
                frame.WriteXml(writer);
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
        }

        public override string TypeName
        {
            get { return Resources.EditableFramesetTypeFriendlyName; }
        }

        protected override void ReadXmlAttributes(XmlReader reader)
        {
            Loop = SerializationUtility.ReadAttribute(reader, "loop", true);
            Fps = SerializationUtility.ReadAttribute(reader, "fps", 30);
        }

        protected override void WriteXmlAttributes(XmlWriter writer)
        {
            writer.WriteAttributeString("loop", Loop.ToString(CultureInfo.CurrentCulture));
            writer.WriteAttributeString("fps", Fps.ToString(CultureInfo.CurrentCulture));
        }
    }
}